<?php


namespace app\api\controller;
use \app\api\model\Message as MessageModel;

use think\Controller;
use think\Request;

class Message extends Controller
{
    protected $middleware = ['Auth'];

    /**
     * 信息通知列表
     */
    public function messageList(Request $require){
        $userid = $require->userId;
        $result = MessageModel::getMessageList($userid);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '查询失败');
        }
    }

    /**
     * 根据id查询信息通知详情
     */
    public function messageDetail(){
        $id  = input('?get.id') ? input('get.id') : 0;
        $result = MessageModel::getMessageById($id);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '查询失败');
        }
    }

    /**
     * 发送信息
     */
    public function sendMessage(Request $require){
        $userid = $require->userId;
        //type   1所有学生   2所有教师和学生   3单个班级的所有学生
        $type  = input('?post.type') ? input('post.type') : 0;
        //班级id  没有传0
        $classid = input('?post.class_id') ? input('post.class_id') : 0;
        //内容
        $messageTitle = input('?post.message_title') ? input('post.message_title') : '';
        $messageContent = input('?post.message_content') ? input('post.message_content') : '';
        if(!$messageTitle || !$messageContent){
            return fail(INVALID_REQUEST, '标题或内容为空');
        }
        $result = MessageModel::sendMessage($userid,$type,$classid,$messageTitle,$messageContent);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '发送失败');
        }
    }
}